<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/fn"%>

<%-- Displays a page selector. The following parameters can be used :
  - pageIndex : optional, the current page index. If not set : 0
  - totalRowsNb : the total number of available rows.
  - nbRowsPerPage : the number of rows per page.
  - formId : the ID of the form used to submit for changing the page.
  - formParam : the name of the param storing the page index value in the form.
  - outOfLabel : the "out of" label
  - goToLabel : the "go to" label
 --%>

<%-- The labels --%>
<c:set var="outOfLabel" value="&nbsp;...&nbsp;${param.outOfLabel}&nbsp;"/>

<%-- Computing values --%>
<c:set var="curIndex" value="${empty param.pageIndex ? 1 : param.pageIndex + 1}"/>
<c:set var="totalPagesNb" value="${param.totalRowsNb / param.nbRowsPerPage}"/>
<c:set var="totalPagesNb" value="${fn:substringBefore(totalPagesNb, '.')}"/>
<c:set var="totalPagesNb"
  value="${param.totalRowsNb mod param.nbRowsPerPage == 0 ? totalPagesNb : totalPagesNb + 1}"/>
<c:set var="groupSize" value="${5}"/>

<%-- The pagin component --%>
<span class="paging">

  <%-- The previous and first links --%>
  <c:if test="${curIndex > 1}">
    <a href="javascript:setPageIndex('1');"><img src="${imgFolder}first.png"></a>
    <a href="javascript:setPageIndex('${curIndex - 1}');"><img style="margin-right:6px" src="${imgFolder}previous.gif"></a>
  </c:if>

  <%-- The current page --%>
  <%-- 1 - Computing the list of the pages to display --%>
  <c:set var="startIndex" value="${curIndex}"/>
  <c:set var="endIndex" value="${curIndex}"/>
  <c:set var="remainingNb" value="${groupSize}"/>

  <c:forEach var="cur" begin="${1}" end="${groupSize}">

    <c:if test="${remainingNb > 0}">

      <c:if test="${startIndex > 1}">
        <c:set var="startIndex" value="${startIndex - 1}"/>
        <c:set var="remainingNb" value="${remainingNb - 1}"/>
      </c:if>

      <c:if test="${endIndex < totalPagesNb}">
        <c:set var="endIndex" value="${endIndex + 1}"/>
        <c:set var="remainingNb" value="${remainingNb - 1}"/>
      </c:if>
    </c:if>
  </c:forEach>

  <%-- Displaying the pages --%>
  <c:forEach var="cur" begin="${startIndex}" end="${endIndex}" varStatus="stat">

    <%-- Displaying the page indices --%>
    <c:choose>
      <%-- The current index corresponds to the current page --%>
      <c:when test="${cur == curIndex}">
        <span class="sel">${curIndex}</span>
      </c:when>
      <c:otherwise>
        <a href="javascript:setPageIndex('${cur}');"
          title="${param.goToLabel} ${cur}">${cur}</a>
      </c:otherwise>
    </c:choose>

    <%-- Adding the separator --%>
    <c:if test="${not stat.last}">
      |
    </c:if>
  </c:forEach>

  <%-- The page out label --%>
  ${outOfLabel}${totalPagesNb}

  <%-- The next and last links --%>
  <c:if test="${curIndex < totalPagesNb}">
    <a href="javascript:setPageIndex('${curIndex + 1}');"><img style="margin-left:6px" src="${imgFolder}next.gif"></a>
    <a href="javascript:setPageIndex('${totalPagesNb}');"><img src="${imgFolder}last.png"></a>
  </c:if>
</span>

<%-- The javascript used to submit the form --%>
<script type="text/javascript">
  function setPageIndex(index){

    $('#${param.formId} #${param.formParam}').val((index - 1));
    $('#${param.formId}').submit();
  }
</script>
